home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 340 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.2 KB

  1. From: clamage@Eng.Sun.COM (Steve Clamage)
  2. Message-ID: <4fdo8b$6ce@engnews1.Eng.Sun.COM>
  3. X-Original-Date: 8 Feb 1996 20:59:55 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 09 Feb 96 15:33:46 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: casting and virtual inheritence
  9. Organization: Sun Microsystems Inc.
  10. References: <4fbt35$9ej@fido.asd.sgi.com>
  11. Reply-To: clamage@Eng.Sun.COM
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMRtpouEDnX0m9pzZAQFpWgF/R5F9HfFtnmznm3IMsZF19K4wXyYlQpXh
  14.     OmFTaTU2isHnEM/uIVIZ9+sUwGnPOJOM
  15.     =yIaJ
  16.  
  17. In article 9ej@fido.asd.sgi.com, cardboard.mti.sgi.com!box.mti.sgi.com!sgi.sgi.
  18. com!ncar.UCAR.EDU!uunet!chatz!chatz (David Chatterton) writes:
  19. >
  20. >There is very little on virtual inheritence in the draft. I cannot find nay
  21. >statement saying that you cannot do this:
  22. >
  23. >class A {};
  24. >class B : public virtual A {};
  25. >
  26. >int main()
  27. >{
  28. >    A* a1 = new A;
  29. >    A* a2 = new B;
  30. >    B* b1 = (B *)A;    // Illegal due to virtual inheritence
  31. >    return 0;
  32. >}
  33.  
  34. In the current draft standard, section 5.4 "Explicit type conversion (cast
  35. notation)" lists exactly those conversions which may be performed, and
  36. down-casting from a virtual base class is not among them.
  37.  
  38. In the ARM, section 5.4 "Explicit Type Conversion" says about the same
  39. thing, the difference being that the four new cast keywords were not
  40. in the language at that time.
  41.  
  42. In other words, the cast noted above has never been allowed in C++, and
  43. still is not.
  44.  
  45. >Yet g++ and cfront won't let you. However, g++ (2.7.0) will let you do this:
  46. >
  47. >    B* b1 = dynamic_cast<B*>(A);
  48.  
  49. >Why can't you force the cast to a B (I guess it has to do with the virtual
  50. >tables)? And is g++ correct in allowing dynamic_cast to do it?
  51.  
  52. Dynamic_cast was created specifically to allow safe down-casting, and in
  53. particular allows you to down-cast pointers or references from a virtual
  54. base class to a derived class. The only requirement is that the base class
  55. have one or more virtual functions. See section 5.2.6 "Dynamic cast" in the
  56. current draft.
  57.  
  58. ---
  59. Steve Clamage, stephen.clamage@eng.sun.com
  60. ---
  61. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  62.   Contact address: std-c++-request@ncar.ucar.edu.  Moderation policy:
  63.   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html. ]
  64.